<?php
namespace App\Services\PingAnPay;

use App\Services\PingAnPay\lib\pingan;
use App\Services\PingAnPay\lib\pingan_quick;


/**
* 短信发送验证码接口，获取支付短信验证码
*/

// const SEND_URL = 'http://testebank.sdb.com.cn/khpayment/UnionAPI_Submit.do';
const SEND_URL = 'https://testebank.sdb.com.cn/khpayment/UnionAPI_SSMS.do';
//如果手机端出现证书过期，可以使用http://testebank.orangebank.com.cn/khpayment/UnionAPI_Submit.do

class UnionApiSsms
{
    

    /**
     * [获取支付短信验证码]
     * @param  [type] $orderId    [订单id]
     * @param  [type] $amount     [订单金额]
     * @param  [type] $paydate    [下单时间]
     * @param  [type] $customerId [商户会员号]
     * @param  [type] $openId     [银行卡开通ID]
     * @return [type]             [description]
     */
    function sendSms($orderId, $amount, $paydate, $customerId, $openId)
    {

        // $pingan = new pingan ();
        $pingan = new pingan_quick();

        $data = array (
                'masterId' => config('pinganpay.masterId'),         //商户号
                'orderId' => $orderId,                              //订单号，严格遵守格式：商户号+8位日期YYYYMMDD+8位流水，需与后续支付交易订单号一致
                'currency' => 'RMB',                                //币种，目前只支持RMB
                'amount' => $amount,                                //订单金额，12整数，2小数
                'paydate' => $paydate,                              //下单时间YYYYMMDDHHMMSS，需与后续支付交易订单号一致
                'customerId' => $customerId,                        //商户会员号
                'OpenId' => $openId,                                //银行卡开通ID
        );

        // $data = array (
        //      'masterId' => 'unionpay01',
        //      'orderId' => 'unionpay01'.date('Ymd'). rand(10000000, 99999999),
        //      'currency' => 'RMB',
        //      'amount' => '100.09',
        //      'paydate' => date('YmdHis'),
        //      'customerId' => '0001',
        //      'OpenId' => 'unionpay012016052743419513',
        // );

        $xml_data = $pingan->array_to_xml ( $data );
        // $merchantCertFile = '../merchant/2000311146.pfx';
        $merchantCertFile = config('pinganpay.merchant_cert_file');

        // 获取签名后的orig和sign
        $orig = $pingan->getOrig ( $xml_data );
        $sign = $pingan->getSign ( $merchantCertFile, $xml_data );

        // 通过curl请求接口
        $parms = 'orig=' . $orig . '&sign=' . $sign;
        $rsponse = $pingan->curl ( SEND_URL, $parms );

        // 通过字符串截取获取orig
        $rsponseData = explode ( 'orig=', $rsponse );
        $rsponse2Data = explode ( 'SDBPAYGATE=', $rsponseData [1] );
        // $rsponse2Data = explode ( 'SDBPAYGATE=', $rsponseData [0] );

        $rsponseSign = explode ( 'sign=', $rsponse );
        $rsponse2Sign = explode ( 'orig=', $rsponseSign [0] );

        // 解码
        $formOrig = $pingan->_base64_url_decode ( $rsponse2Data [0] );
        $formSign = $pingan->_base64_url_decode ( $rsponse2Sign [0] );
        // $tTrustPayCertFile = 'merchant/paygate.cer';
        $tTrustPayCertFile = config('pinganpay.t_trust_pay_cert_file');

        //2、验证签名是否正确
        $result = $pingan->validate($formOrig, $formSign, $tTrustPayCertFile);
        if ($result) {
            echo 'success';
        } else {
            exit('error');
        }
        // $xml_dom = str_replace("GBK", "utf-8", $formOrig);
        // $result = $pingan->xml_to_array($xml_dom);
        // 输出结果
        $formOrig = iconv("GBK", "UTF-8", $formOrig);
        $orig_data = $pingan->xml_to_array_bak($formOrig);

        /**
         * { ["errorCode"]=> string(0) "" 
         * ["errorMsg"]=> string(0) "" 
         * ["masterId"]=> string(10) "unionpay01" 
         * ["orderId"]=> string(26) "unionpay012016052758494889"
         *  ["currency"]=> string(3) "RMB" 
         *  ["amount"]=> string(6) "100.09" 
         *  ["status"]=> string(2) "01" 
         *  ["paydate"]=> string(14) "20160527031756" 
         *  ["customerId"]=> string(4) "0001" }
         * */

        //var_dump($orig_data);
        echo json_encode($orig_data);
    }
}


?>
